﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using COM.OA.BLL;
using System.Collections.Generic;
using COM.OA.Entity;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
       
    }
    //清空
    protected void Button6_Click(object sender, EventArgs e)
    {
        this.btn_xiaoxi.Text = "";
        this.txt_jieshouren.Text = "";     
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        //接收标题
        string biaoti = this.Request.Form["txt_biaoti"];
        //取部门
        string bumen = this.ddl_bumen.Text;
        //取对象
        string duixiang = this.ddl_duixiang.Text;
        //取消息内容
        string xiaoxi = this.btn_xiaoxi.Text;
        //取接收人，
        string jieshouren1 = this.txt_jieshouren.Text;
        string jieshouren=this.ddl_duixiang.Text;
        if (duixiang == null || duixiang == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "对象不能为空"));
            this.ddl_duixiang.Focus();
            return;
        }
        else if (jieshouren1 == null || jieshouren1 == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "接收人不能为空"));
            this.txt_jieshouren.Focus();
            return;
        }
        
        else if (xiaoxi == null || xiaoxi == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "消息不能为空"));
            this.btn_xiaoxi.Focus=true;
            return;
        }
      
        //取对象
        //string duixiang = this.ddl_duixiang.Text;       
        else
        {
            //设置where语句,将接收人格式化成SQL语句查接收人ID
            String where = "em_name='{0}'";
            where = string.Format(where, jieshouren);
            IList<employee> list = employeeBLL.Select(where);
            employee emp = list[0];
            int jieshoureni = emp.em_id;
            String where1 = "u_em_id='{0}'";
            where1 = string.Format(where1, jieshoureni);
            IList<users> list1 = usersBLL.Select(where1);
            users uu=list1[0];
            int jieshourenid = uu.u_id;
            
            //取当前时间
            DateTime time = DateTime.Now;
            //取发送人
            //取登陆人信息 
            COM.OA.Entity.users loginuser = Session["loginuser"] as COM.OA.Entity.users;
            //如果取不到跳转到登陆页
            if (loginuser == null)
            {
                this.Response.Write(string.Format(GetRedirect.ALLREDIRECT, "../login.aspx"));
            }
            else
            {
                int sendid = loginuser.u_id;
                //往消息实体类里存值
                message mage = new message();
                mage.m_title = biaoti;
                mage.m_content = xiaoxi;
                mage.m_releasetime = time;
                mage.m_sendu_id = sendid;
                mage.m_u_id = jieshourenid;
                mage.m_g_id = 1;
                //回执 是：1  否：0
                mage.m_isreturn = 0;
                //往数据库里插入数据
                messageBLL.Insert(mage);
                this.Response.Write(string.Format(GetRedirect.WINALERT, "插入成功"));
            }
        }
    }
    protected void ddl_bumen_SelectedIndexChanged(object sender, EventArgs e)
    {
        //2级连动，取部门
        String bumen=this.ddl_bumen.Text;
        // String where = "em_dept_id='{0}'";
        //  where = string.Format(where,bumen);
        //   IList<employee> list = employeeBLL.Select(where);
        //根据部门查询子数据

        SqlParameter parambumen = new SqlParameter("@deptid", bumen);
         IList<liandongren> lists = BLLGeneric.Select<liandongren>(CommandType.StoredProcedure, "liandong", parambumen);
        //配置数据源
         this.ddl_duixiang.DataSource = lists;
        //绑定TEXT，VALUE属性
         this.ddl_duixiang.DataTextField = "Em_name";
         this.ddl_duixiang.DataValueField = "Em_name";
        //this.ddl_duixiang.DataValueField = "Em_id";
        //绑定
         this.ddl_duixiang.DataBind();

    }
    
    protected void Button4_Click(object sender, EventArgs e)
    {
        this.txt_jieshouren.Text = "";
    }
    //添加接收人
    protected void Button1_Click(object sender, EventArgs e)
    {

        string duixiang=this.ddl_duixiang.Text;

        this.txt_jieshouren.Text = duixiang;
    }
    //部门群发
    protected void Button3_Click(object sender, EventArgs e)
    {
       this.Response.Write(string.Format(GetRedirect.REDIRECT,"bumenqunfa.aspx"));

        ////取部门
        //string bumen=this.ddl_bumen.Text;
        //SqlParameter parambumen = new SqlParameter("@deptid", bumen);
        //IList<liandongren> lists = BLLGeneric.Select<liandongren>(CommandType.StoredProcedure, "liandong", parambumen);
       
        ////取消息内容
        //string xiaoxi = this.btn_xiaoxi.Text;       
        ////接收标题
        //string biaoti = this.Request.Form["txt_biaoti"];
        ////取当前时间
        //DateTime time = DateTime.Now;
        ////取发送人
        //int sendid = 1;
        ////往消息实体类里存值
        //message mage = new message();
        //mage.m_title = biaoti;
        //mage.m_content = xiaoxi;
        //mage.m_releasetime = time;
        //mage.m_sendu_id = sendid;
        
        //mage.m_g_id = 1;
        ////回执 是：1  否：0
        //mage.m_isreturn = 0;
        

        ////循环取接收人
        //if (lists.Count > 0)
        //{
        //    foreach (liandongren jdr in lists)
        //    {
        //        int jieshourenid = jdr.Em_id;
        //        mage.m_u_id = jieshourenid;
        //        //往数据库里插入数据
        //        messageBLL.Insert(mage);
        //    }
        //}
        //this.Response.Write(string.Format(GetRedirect.WINALERT,"插入成功"));
        
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        this.Response.Write(string.Format(GetRedirect.REDIRECT, "bumenqunfa.aspx"));
    }
}
